 

 
var multiparty = require('multiparty');

var util = require('util'); 
module.exports = function (UC, source, config, ArFor, cache, package) {
    var UserControl = UC;
    var getTitelorFormat = function (str, obj) {

        var ar = str.split('.');

        var fileformat = ar[ar.length - 1];

        ar.splice(ar.length - 1, 1);

        obj.title = ar.join('.');

        obj.fileformat = fileformat;

        return obj;
    };
    var PluginFile = function (req, Callback) {
        var info = UserControl.GetForm(req.session.key, {
            UploadFile: 1
        });
        var FileType = 1;

        var obj = {
            "SQL": " SELECT rjd.UploadFile FROM roles_jurisdictions AS rjd INNER JOIN user AS ud ON rjd.roles_id = ud.roles_id where rjd.UploadFile='{UploadFile}' and ud.id={user_id}",
            "type": "row"
        }
        new source(obj, info, function (db) {
            if (db) {
                var uploadDir = config.Files.FileCache; 
                var form = new multiparty.Form({
                    uploadDir: uploadDir
                });
                //上传完成后处理
                form.parse(req, function (err, fields, files) {
                    var filesTmp = JSON.stringify(files, null, 2);
                    var ut = util.inspect({
                        fields: fields,
                        files: filesTmp
                    });
                    if (fields && fields.FileType) {
                        FileType = fields.FileType;
                    }
                    if (1 == 1) {
                        var _file = []
                        if (files["files[]"] && files["files[]"].length > 0) {
                            _file = files["files[]"];
                        } else if (files.files && files.files.length > 0) {
                            _file = files.files
                        }
                        new ArFor(_file, function () {
                            var value = {
                                error: 1
                            };
                            value.error = 0;
                            Callback(value);
                        }).forEach(function (item, next) {
                            var _path = item.path;
                            var src = _path.split('/')
                            if (src.length == 1) {
                                src = _path.split('\\')
                            };
                            src = src[src.length - 1];

                            var info = getTitelorFormat(item.originalFilename, {
                                src: src,
                                FileType: FileType
                            });

                            info = UserControl.GetForm(req.session.key, info);

                            obj = {
                                "SQL": "INSERT INTO [files] (Coinkey,[src],[creation],[title],[fileformat],[FileType],[user_id]) values ('{Coinkey}','{src}',datetime('now','+8 hour') ,'{title}','{fileformat}',{FileType},{user_id})",
                                "type": "add"
                            };
                            new source(obj, info, function (db) {
                                next();
                            })
                        })
                    }
                });
            } else {
                var value = {
                    error: 1
                };
                Callback(value);
            }
        })
    };
    var router = function (req, res, next) {
       
    var query = req.query;
        if (query.FileType) {
            PluginFile(req, function (value) {
                res.Json(value);
            });
        } else {
            var value = {
                error: 1
            };
            res.Json(value);
        }
    }
    return router;
}